package mdm.com.sap;

import java.util.ArrayList;
import java.util.List;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;

import mdm.com.sap.model.Zstock_info;

public class GetStockInfo {

    // <param name="TankNo">缸号</param>
    // <param name="Color">颜色</param>
    // <param name="Material">物料</param>
    // <param name="BoxValue">网格值</param>
    // <param name="LocationNo">仓位</param>
    // <param name="Barcode">条码</param>
    // <param name="PONo">采购PO</param>
    /**
     * 参数名称
     * I_CANGHAO
     * I_COLOR
     * I_MATNR
     * I_3ASIZE
     * I_LGORT
     * I_BARC
     * I_EBELN
     * I_SELECT_BY_PO
     * I_IPO
     * ZDFLQ
     * ZBUKRS
     * @param TankNo
     * @param Color
     * @param Material
     * @param BoxValue
     * @param LocationNo
     * @param Barcode
     * @param PONo
     * @return
     */
    public static List<Zstock_info> GetStockInfo_Call(String TankNo, String Color, String Material, String BoxValue,
            String LocationNo, String Barcode, String PONo) {
        List<Zstock_info> myZSTOCK_INFOList = new ArrayList<Zstock_info>();
        try {
            JCoFunction function = RfcManager.getFunction("ZBAPI_GET_STOCK_INFO_BY_BC");
            JCoParameterList importParam = function.getImportParameterList();
//            if (!TankNo.equals("") && TankNo != null) {
//                JCoTable So_MATNR = function.getTableParameterList().getTable("I_MATNR");
//                // Create new MATNRSELECTION row
//                So_MATNR.appendRow();
//                // Populate current MATNRSELECTION row with data from list
//                So_MATNR.setValue("SIGN", "I");
//                So_MATNR.setValue("OPTION", "EQ");
//                So_MATNR.setValue("LOW", TankNo);
//                So_MATNR.setValue("HIGH", "");
//            }
//            if (!Color.equals("")) {
//                importParam.setValue("Color", Color);
//            }
//            if (!Material.equals("")) {
//                importParam.setValue("MaterialNo", Material);
//            }
//            if (!BoxValue.equals("")) {
//                importParam.setValue("Grid_Value", BoxValue);
//            }
//            if (!LocationNo.equals("")) {
//                importParam.setValue("LocationID", LocationNo);
//            }
//            if (!Barcode.equals("")) {
//                importParam.setValue("Barcode", Barcode);
//            }
//            if (PONo.equals("")) {
                importParam.setValue("I_SELECT_BY_PO", "");
//            } else {
////                importParam.setValue("PONo", PONo);
//                importParam.setValue("I_SELECT_BY_PO", "X");
//            }
            function.execute(RfcManager.getDestination());
            // 判断接口是否连接成功（默认为false）
            boolean bCallSuccess = false;
            JCoTable ReturnTable = function.getTableParameterList().getTable("ZRETURN");
            String returnType = ReturnTable.getString("TYPE");
            switch (returnType) {
                case "S": // S 成功
                    bCallSuccess = true;
                    break;
                default: // E 错误,W 警告,I 信息,A 中断
                    break;
            }
            System.out.println("============returnType============="+returnType);
            if (bCallSuccess) {
                JCoTable RfcZStockInfo = function.getTableParameterList().getTable("ZSTOCK_INFO");
                for (int j = 0; j < RfcZStockInfo.getNumRows(); j++) {
                    RfcZStockInfo.setRow(j);
                    Zstock_info info = new Zstock_info();
                    info.setMatmr(RfcZStockInfo.getString("MATNR"));
                    info.setMaktx(RfcZStockInfo.getString("MAKTX"));
                    info.setJ_3asize(RfcZStockInfo.getString("J_3ASIZE"));
                    info.setAtwtb(RfcZStockInfo.getString("ATWTB"));
                    info.setWerks(RfcZStockInfo.getString("WERKS"));
                    info.setLgort(RfcZStockInfo.getString("LGORT"));
                    info.setMenge(RfcZStockInfo.getString("MENGE"));
                    info.setMeins(RfcZStockInfo.getString("MEINS"));
                    info.setZsto_type(RfcZStockInfo.getString("ZSTO_TYPE"));
                    info.setZbar_code(RfcZStockInfo.getString("ZBAR_CODE"));
                    info.setCharg(RfcZStockInfo.getString("CHARG"));
                    info.setWgbez(RfcZStockInfo.getString("WGBEZ"));
                    info.setZbatch1_po(RfcZStockInfo.getString("ZBATCH1_PO"));
                    info.setVbeln(RfcZStockInfo.getString("VBELN"));
                    info.setPosnr(RfcZStockInfo.getString("POSNR"));
                    info.setZbatch1_qi(RfcZStockInfo.getString("ZBATCH1_QI"));
                    info.setZbatch1_batch(RfcZStockInfo.getString("ZBATCH1_BATCH"));
                    info.setZbatch1_gr_date(RfcZStockInfo.getString("ZBATCH1_GR_DATE"));
                    info.setZmenge_gr(RfcZStockInfo.getString("ZMENGE_GR"));
                    info.setZmenge_gi(RfcZStockInfo.getString("ZMENGE_GI"));
                    info.setAufnr(RfcZStockInfo.getString("AUFNR"));
                    System.out.println("---------------------"+info);
                    myZSTOCK_INFOList.add(info);
                }
            }
        } catch (JCoException e) {
            e.printStackTrace();
        }

        return myZSTOCK_INFOList;
    }

}
